import akshare as ak
import pandas as pd

# 获取股票历史行情数据
stock_zh_a_hist_df = ak.stock_zh_a_hist(
    symbol="002639", 
    period="daily",
    start_date="20200425", 
    end_date="20250425", 
    adjust=""
)

# 打印数据
print(stock_zh_a_hist_df)

# 保存数据到 CSV 文件
if not stock_zh_a_hist_df.empty:
    # 构建文件名，包含股票代码和日期范围
    filename = f"stock_{stock_zh_a_hist_df['股票代码'][0]}_{stock_zh_a_hist_df['日期'].iloc[0]}_{stock_zh_a_hist_df['日期'].iloc[-1]}.csv"
    
    try:
        stock_zh_a_hist_df.to_csv(filename, index=False, encoding='utf-8-sig')
        print(f"数据已成功保存到文件: {filename}")
    except Exception as e:
        print(f"保存文件时出错: {e}")
else:
    print("未获取到数据，无法保存文件。")